
//
//  UserRequestManager.m
//  much
//
//  Created by weijunjie on 2019/1/2.
//  Copyright © 2019年 iOS. All rights reserved.
//

#import "MuchUserRequestManager.h"

@implementation MuchUserRequestManager

+ (void)userLoginRequestCaptchaWithIdentity:(NSString *)identity
                               successBlock:(RequestSuccess)successBlock
                               failureBlock:(RequestFailed)failureBlock {
    NSString *urlStr = [NSString stringWithFormat:@"%@%@/login/captcha",UserLoginRequestCaptchaAPI,identity];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setObject:@(4) forKey:@"length"];
    [MuchHttpRequestManager sendHttpRequestWithAPI:urlStr method:HttpRequestGet arguments:param andSuccessCallback:^(id responseObject) {
        if (successBlock) {
            successBlock(responseObject);
        }
    } andFailCallback:^(NSString *error) {
        [SVProgressHUD showErrorWithStatus:error];
    }];
}

+ (void)userLoginWithIdentity:(NSString *)identity
                      captcha:(NSString *)captcha
                 successBlock:(RequestSuccess)successBlock
                 failureBlock:(RequestFailed)failureBlock {
    NSMutableDictionary *pagram = [NSMutableDictionary dictionary];
    if (identity.hasValue) [pagram setObject:identity forKey:@"identity"];
    if (captcha.hasValue) [pagram setObject:captcha forKey:@"captcha"];
    
    NSString *pagramStr = [NSString dictionaryToJSONString:pagram];
    NSData *data = [pagramStr dataUsingEncoding:NSUTF8StringEncoding];
    
    [MuchHttpRequestManager sendHttpRequestWithAPI:UserLoginAPI method:HttpRequestPost arguments:data andSuccessCallback:^(id responseObject) {
        if (successBlock) {
            successBlock(responseObject);
        }
    } andFailCallback:^(NSString *error) {
        [SVProgressHUD showErrorWithStatus:error];
    }];
}

+ (void)userLoginOut:(RequestSuccess)successBlock
        failureBlock:(RequestFailed)failureBlock {
    NSString *urlStr = [NSString stringWithFormat:@"%@", UserLoginAPI];
    [MuchHttpRequestManager sendHttpRequestWithAPI:urlStr method:HttpRequestDelete arguments:nil andSuccessCallback:^(id responseObject) {
        if (successBlock) {
            successBlock(responseObject);
        }
    } andFailCallback:^(NSString *error) {
        [SVProgressHUD showErrorWithStatus:error];
    }];
}

+ (void)getUserBaseMessage:(RequestSuccess)successBlock
              failureBlock:(RequestFailed)failureBlock {
    NSString *urlStr = [NSString stringWithFormat:@"%@", UserBaseInfoAPI];
    [MuchHttpRequestManager sendHttpRequestWithAPI:urlStr method:HttpRequestGet arguments:nil andSuccessCallback:^(id responseObject) {
        UserBaseInfoModel *model = [UserBaseInfoModel modelWithDictionary:responseObject];
        if (successBlock) {
            successBlock(model);
        }
    } andFailCallback:^(NSString *error) {
        [SVProgressHUD showErrorWithStatus:error];
    }];
}

+ (void)editUserBaseMessage:(NSString *)nickname
                  signature:(NSString *)signature
                   areaName:(NSString *)areaName
                    areaSid:(NSString *)areaSid
            avatarImagePath:(NSString *)avatarImagePath
             avatarImageSid:(NSString *)avatarImageSid
               successBlock:(RequestSuccess)successBlock
               failureBlock:(RequestFailed)failureBlock {
    NSMutableDictionary *pagram = [NSMutableDictionary dictionary];
    if (nickname.hasValue) [pagram setObject:nickname forKey:@"nickname"];
    if (signature.hasValue) [pagram setObject:signature forKey:@"signature"];
    if (areaName.hasValue) {
        [pagram setObject:areaName forKey:@"areaName"];
        [pagram setObject:areaSid forKey:@"areaSid"];
    }
    if (avatarImagePath.hasValue) {
        [pagram setObject:avatarImagePath forKey:@"avatarImagePath"];
        [pagram setObject:avatarImageSid forKey:@"avatarImageSid"];
    }
    
    NSString *pagramStr = [NSString dictionaryToJSONString:pagram];
    NSData *data = [pagramStr dataUsingEncoding:NSUTF8StringEncoding];
    
    [MuchHttpRequestManager sendHttpRequestWithAPI:EditUserBaseInfoAPI method:HttpRequestPut arguments:data andSuccessCallback:^(id responseObject) {
        if (successBlock) {
            successBlock(responseObject);
        }
    } andFailCallback:^(NSString *error) {
        if (failureBlock) {
            failureBlock(error);
            [SVProgressHUD showErrorWithStatus:error];
        }
    }];
}

@end
